<?php
/*
 * 第三方用户信息控制器
 */
class UserController extends BasicController {
    
    private function init(){
        Yaf_Registry::get('tokenPlugin')->checkOtherAuth();
    }

    public function combinationAction(){
        $params = $this->getPost();
        
        $utoken = isset($params['utoken']) ? $params['utoken'] : '';
        
        $user_id = tokenToUid($utoken);
        
        $return['code'] = "0";
        $return['msg'] = '操作成功！';
        $return['data'] = [];
        
        if(!$user_id){//没有传递要查询的user_id值
            $return['code'] = "1";
            $return['msg'] = '参数错误';
            
            echo json_encode($return);
            return false;
        }
        
        // 查询 user_role_relation 表判断账号类型
        $UserRoleRelationModel = Helper::load('User_role_relation');
        
        $userRoleRelationInfo = $UserRoleRelationModel->Where(['user_id' => $user_id])->SelectOne();
        if(empty($userRoleRelationInfo) || !is_array($userRoleRelationInfo)){//找不到对应的用户
            $return['code'] = "2";
            $return['msg'] = '用户不存在';
            
            echo json_encode($return);
            return false;
        }
        
        $roleId = $userRoleRelationInfo['role_id'];
        
        $UserDetailModel = Helper::load('User_detail');
        $userDetailInfo = $UserDetailModel->Where(['user_id' => $user_id])->SelectOne();
        
        $return['data']['name'] = isset($userDetailInfo['user_realname']) ? $userDetailInfo['user_realname'] : '';//名字
        
        $schoolMainModel = Helper::load('School_main');
        $classMainModel = Helper::load('Class_main');
        $classMemberModel = Helper::load('Class_member');
        
        $sm_id = $userDetailInfo['sm_id'];
        
        $smInfo = $schoolMainModel->Where(['sm_id' => $sm_id])->SelectOne();

        $return['data']['school']['id'] = $sm_id;//学校id
        $return['data']['school']['name'] = isset($smInfo['sm_name']) ? $smInfo['sm_name'] : '';//学校名
        
        $return['data']['user_id'] = $user_id;//用户id
        
        //此处记录用户登录人机英语平台的登陆信息
        $userLoginLogModel = Helper::load('User_login_log');
        $ullData['ull_user_id'] = $user_id;
        $ullData['ull_token'] = $utoken;
        $ullData['ull_type'] = 1;
        $ullData['ctime'] = CUR_TIMESTAMP;
        $userLoginLogModel->Insert($ullData);
        
        if($roleId == 22){//教师 
            //学校名称 班级名称 名字 年级id 班级id 账号类型 用户id
            $classRes = $classMemberModel->Where(['cm_type' => 22, 'isdelete' => 0, 'user_id' => $user_id])->Select();
            
            if(!empty($classRes) && is_array($classRes)){
                foreach($classRes as $item){
                    $classId = $item['class_id'];
                    $classInfo = $classMainModel->Where(['class_id' => $classId])->SelectOne();
                    $return['data']['class'][] = [
                        'id'    =>  $classId,
                        'name'  =>  isset($classInfo['class_name']) ? $classInfo['class_name'] : '',
                        'grade' =>  isset($classInfo['grade_num']) ? $classInfo['grade_num'] : 0
                    ];//班级id
                }
            }
            else{
                $return['data']['class'][] = [
                    'id'    =>  0,
                    'name'  =>  '',
                    'grade' =>  0
                ];//班级id
            }
            
            $return['data']['user_role'] = 'T';//教师
            
            $return['data']['authorization'] = [];
        }
        else{//学生
            //学校名称 班级名称 名字 年级id 班级id 账号类型 用户id
            $class_id = $userDetailInfo['class_id'];
            
            $classInfo = $classMainModel->Where(['class_id' => $class_id])->SelectOne();
            $return['data']['class'][] = [
                'id'    =>  $class_id,
                'name'  =>  isset($classInfo['class_name']) ? $classInfo['class_name'] : '',
                'grade' =>  isset($classInfo['grade_num']) ? $classInfo['grade_num'] : 0
            ];//班级id
            
            $return['data']['user_role'] = 'S';//学生
            
            $ActivationkeyUseModel = Helper::load('Activationkey_use');

            $list = $ActivationkeyUseModel->Where(['au_user_id' => $user_id, 'is_valid' => 1])->Select();
            
            $data = [];
            if(!empty($list) && is_array($list)){//找不到 / 无效
                foreach($list as $item){
                    $data[] = [
                        'grade' => $item['au_grade'],
                        'stime' => date('Y-m-d H:i:s', $item['au_stime']),
                        'etime' => date('Y-m-d H:i:s', $item['au_etime'])
                    ];
                }
            }

            $return['data']['authorization'] = $data;
        }
        
        echo json_encode($return);
        return false;
    }
    
    /*
    * 人机英语 - 账号信息 Api
    */
    public function accountinfoAction(){
        $params = $this->getPost();
        
        $utoken = isset($params['utoken']) ? $params['utoken'] : '';
        
        $user_id = tokenToUid($utoken);
        
        $return['code'] = "0";
        $return['msg'] = '操作成功！';
        $return['data'] = [];
        
        if(!$user_id){//没有传递要查询的user_id值
            $return['code'] = "1";
            $return['msg'] = '参数错误';
            
            echo json_encode($return);
            return false;
        }
        
        // 查询 user_role_relation 表判断账号类型
        $UserRoleRelationModel = Helper::load('User_role_relation');
        
        $userRoleRelationInfo = $UserRoleRelationModel->Where(['user_id' => $user_id])->SelectOne();
        if(empty($userRoleRelationInfo) || !is_array($userRoleRelationInfo)){//找不到对应的用户
            $return['code'] = "2";
            $return['msg'] = '用户不存在';
            
            echo json_encode($return);
            return false;
        }
        
        $roleId = $userRoleRelationInfo['role_id'];
        
        $UserDetailModel = Helper::load('User_detail');
        $userDetailInfo = $UserDetailModel->Where(['user_id' => $user_id])->SelectOne();
        
        $return['data']['name'] = isset($userDetailInfo['user_realname']) ? $userDetailInfo['user_realname'] : '';//名字
        
        $schoolMainModel = Helper::load('School_main');
        $classMainModel = Helper::load('Class_main');
        $classMemberModel = Helper::load('Class_member');
        
        $sm_id = $userDetailInfo['sm_id'];
        
        $smInfo = $schoolMainModel->Where(['sm_id' => $sm_id])->SelectOne();

        $return['data']['school']['id'] = $sm_id;//学校id
        $return['data']['school']['name'] = isset($smInfo['sm_name']) ? $smInfo['sm_name'] : '';//学校名
        
        $return['data']['user_id'] = $user_id;//用户id
        
        //此处记录用户登录人机英语平台的登陆信息
        $userLoginLogModel = Helper::load('User_login_log');
        $ullData['ull_user_id'] = $user_id;
        $ullData['ull_token'] = $utoken;
        $ullData['ull_type'] = 1;
        $ullData['ctime'] = CUR_TIMESTAMP;
        $userLoginLogModel->Insert($ullData);
        
        if($roleId == 22){//教师 
            //学校名称 班级名称 名字 年级id 班级id 账号类型 用户id
            $classRes = $classMemberModel->Where(['cm_type' => 22, 'isdelete' => 0, 'user_id' => $user_id])->Select();
            
            if(!empty($classRes) && is_array($classRes)){
                foreach($classRes as $item){
                    $classId = $item['class_id'];
                    $classInfo = $classMainModel->Where(['class_id' => $classId])->SelectOne();
                    $return['data']['class'][] = [
                        'id'    =>  $classId,
                        'name'  =>  isset($classInfo['class_name']) ? $classInfo['class_name'] : '',
                        'grade' =>  isset($classInfo['grade_num']) ? $classInfo['grade_num'] : 0
                    ];//班级id
                }
            }
            else{
                $return['data']['class'][] = [
                    'id'    =>  0,
                    'name'  =>  '',
                    'grade' =>  0
                ];//班级id
            }
            
            $return['data']['user_role'] = 'T';//教师
        }
        else{//学生
            //学校名称 班级名称 名字 年级id 班级id 账号类型 用户id
            $class_id = $userDetailInfo['class_id'];
            
            $classInfo = $classMainModel->Where(['class_id' => $class_id])->SelectOne();
            $return['data']['class'][] = [
                'id'    =>  $class_id,
                'name'  =>  isset($classInfo['class_name']) ? $classInfo['class_name'] : '',
                'grade' =>  isset($classInfo['grade_num']) ? $classInfo['grade_num'] : 0
            ];//班级id
            
            $return['data']['user_role'] = 'S';//学生
        }
        
        echo json_encode($return);
        return false;
    }
    
    /*
    * 人机英语 - 授权信息 Api
    */
    public function authorizationinfoAction(){
        $params = $this->getPost();
        
//        $user_id = isset($params['id']) ? $params['id'] : 0;
        $utoken = isset($params['utoken']) ? $params['utoken'] : '';
        
        $user_id = tokenToUid($utoken);
        
        $return['code'] = "0";
        $return['msg'] = '操作成功！';
        $return['data'] = [];
        
        if(!$user_id){//没有传递要查询的user_id值
            $return['code'] = "1";
            $return['msg'] = '参数错误';
            
            echo json_encode($return);
            return false;
        }
        
        $ActivationkeyUseModel = Helper::load('Activationkey_use');

        $list = $ActivationkeyUseModel->Where(['au_user_id' => $user_id, 'is_valid' => 1])->Select();
        
        $data = [];
        if(!empty($list) && is_array($list)){//找不到 / 无效
            foreach($list as $item){
                $data[] = [
                    'grade' => $item['au_grade'],
                    'stime' => date('Y-m-d H:i:s', $item['au_stime']),
                    'etime' => date('Y-m-d H:i:s', $item['au_etime'])
                ];
            }
            
        }
        else{
            $return['code'] = "2";
            $return['msg'] = '用户尚未被授权';
            
            echo json_encode($return);
            return false;
        }
        
        $return['data'] = $data;
        
        echo json_encode($return);
        return false;
    }
    
    /*
    * 题库 - 账号信息 Api
    */
    public function userinfoAction(){
        $params = $this->getPost();
        
        $utoken = isset($params['utoken']) ? $params['utoken'] : '';
        
        $user_id = tokenToUid($utoken);
        
        $return['code'] = "0";
        $return['msg'] = '操作成功！';
        $return['data'] = [];
        
        if(!$user_id){//没有传递要查询的user_id值
            $return['code'] = "1";
            $return['msg'] = '参数错误';
            
            echo json_encode($return);
            return false;
        }
        
        //此处加入缓存判断，如果此处有缓存则取缓存，如果没有缓存则查询数据库并将检索信息加载到缓存
        if(defined("IS_MONGO" && IS_MONGO)){
            Yaf_Loader::import(LIB_PATH.'/Cmongo.php');
            $m = new Cmongo();
            $mKey = 'other_user_userinfo_uid_'.$user_id;
            $userMongoRes = $m->getOne('Data', ['key' => $mKey]);//带查询条件获取唯一一条数据 如果没有该数据的话返回NULL
            //如果此处有缓存则取缓存直接返回
            if(!empty($userMongoRes) && is_array($userMongoRes)){
                echo $userMongoRes['val'];
                return false;
            }
        }
        
        //走到这里说明没找到缓存
        $UserDetailModel = Helper::load('User_detail');
        $userDetailInfo = $UserDetailModel->Where(['user_id' => $user_id])->SelectOne();
        
        if(!$userDetailInfo){
            $return['code'] = "2";
            $return['msg'] = '用户不存在';
            
            echo json_encode($return);
            return false;
        }
        
        $return['data']['user_id'] = $userDetailInfo['user_id'] ? $userDetailInfo['user_id'] : '';
        $return['data']['sm_id'] = $userDetailInfo['sm_id'] ? $userDetailInfo['sm_id'] : '';
        $return['data']['class_id'] = $userDetailInfo['class_id'] ? $userDetailInfo['class_id'] : '';
        
        $return['data']['grade_num'] = '';
        $return['data']['class_num'] = '';
        //拉取对应用户的年级编号和班级编号
        if($userDetailInfo['class_id']){
            $ClassMainModel = Helper::load('Class_main');
            $classInfo = $ClassMainModel->Where(['class_id' => $userDetailInfo['class_id']])->SelectOne();
            
            if(!empty($classInfo) && is_array($classInfo)){
                $return['data']['grade_num'] = $classInfo['grade_num'] ? $classInfo['grade_num'] : '';
                $return['data']['class_num'] = $classInfo['class_num'] ? $classInfo['class_num'] : '';
            }
        }
        
        $jsonRes = json_encode($return);
        
        if(defined("IS_MONGO" && IS_MONGO)){
            //此处将json信息塞到缓存中去
            $m->toInsert('Data', ['key' => $mKey, 'val' => $jsonRes, 'ctime' => CUR_TIMESTAMP, 'field' => 'user_id', 'fdata' => $user_id]);//新增操作
        }
        
        echo $jsonRes;
        return false;
    }
    
}